<html>
<head>
<title>NES ASM - Day 7</title>
</head>
<body bgcolor="lavender">
<h1 align="center" style="color: LightCoral; width: 100%; filter:shadow()">Day 7 - Using Memory</h1>
<pre>


</pre>
<h2 align="center" style="color: LightCoral;"><b>Where?</b></h2>
<p style="color: green;"><pre><b>
	Before we do anything else it would probably be best to show how to
use memory for our own data. This data could be anything from just a number you
need to store or X/Y values for sprites. For this data we'll use free memory at
$0000 (CPU).

<h3 align="center" style="color: LightCoral;"><b>How?</b></h3>

	We'll ORG at $0000 in our Code Bank 0 and label some memory before ORGing to
$8000 where our code starts. To label some memory (a.k.a making a variable) we use the
assembler operation .db like so:

; note this is only a small code piece to demonstrate memory labeling syntax.

	.bank 0
	.org $0000

label1:    .db   0     
byte1:     .db   $A5   ; notice that with Define Byte (.db) we don't need the '#' .

	.org $8000

Start:     ; from here on would be code as usual

	Since label1 and byte1 are infact labels, they do need to start at the first
colon (not tabbed in).

<h3 align="center" style="color: LightCoral;"><b>Loading Value of Our "Variables"</b></h3>

	To load the value of one of our "Variables", just use a regular Load instruction
with the label's name. Like so:

	;assume label1 and byte1 have been declared like shown above.

	lda label1  ; load A with label1's value   	
	ldx label1  ; load X with label1's value
	ldy label1  ; I hope you get the idea now

	lda byte1  ; load A with byte1's value

	That's it for getting the value. Storing is just as easy. Even though you
can figure it out yourself, I'll go through the same steps with storing.
  
<h3 align="center" style="color: LightCoral;"><b>Storing Into Our Variables</b></h3>

	Storing is just like loading except the other way around (putting, not getting).
It's done like this:

	;again assume label1 was declared like shown before

	sta label1  ; store A's value into label1
	stx label1  ; store X's value into label1
	sty label1 ; store Y's value into label1

	stx byte1  ; you should get it by now, I hope.

	Simple, isn't it?

<h3 align="center" style="color: LightCoral;"><b>Some Important Facts</b></h3>
<h4 align="center" style="color: LightBlue;"><b>That I Waited Until The End To Mention</b></h4>
	
	The main thing I want to mention here is that we can define more than just bytes.
We can also define words, but since the NES's CPU works in only 8bits, this is a little
more than I want to talk about right now. 

	Also, I haven't tried it, but if you want to increment or decrement (Add or Sub 1 
respectfully), you could probably go something like this:

	inc label1  ; add 1 to label1

	dec label1  ; sub 1 from label1

<h3 align="center" style="color: LightCoral;"><b>This Day In Review</b></h3>

	I have no idea what's in store for tomorrow, since I need to complete Day 13 of
the GBA tutorial. I hope you had fun today. Why don't you try moving a sprite with the
keypad?
	
	Until Next Time,
		-Mike H a.k.a GbaGuy

<center><a href="nesasm.htm">Intro</a> - <a href="day8n.htm">Day 8</a></center>


</b></pre></p>

<br />
<br />

</body>
</html>
